package routing;

import org.osmdroid.views.overlay.MyLocationOverlay;

import osm.OsmReader;
import android.location.Location;
import android.util.Log;

public class StartFinder {
	
	int currentClosestID;
	float currentClosestDistance=9999;
	float thisDistance;
	String TAG="URNav";
	
	
//	Gets the closest node to own location
	public int getClosestNode(MyLocationOverlay myLocOverlay, OsmReader myOsmReader){
		
		Location thisLocation=myLocOverlay.getLastFix();
		for (int i=0; i<myOsmReader.getNodes().getSize(); i++) {
			
			Location checkLocation= new Location("x");
			checkLocation.setLatitude(myOsmReader.getNodes().getDlati(i));
			checkLocation.setLongitude(myOsmReader.getNodes().getDlongi(i));
			thisDistance=thisLocation.distanceTo(checkLocation);
//			Log.i(TAG, "Distance: " + String.valueOf(currentClosestDistance));
			if (thisDistance < currentClosestDistance) {
				currentClosestDistance=thisDistance;
				currentClosestID=i;
		
			}
//			Log.i(TAG, "Node OSM ID: " + String.valueOf(myOsmReader.getNodes().getOsmID(i)) + " Distance: " + String.valueOf(thisDistance) + " CClosest " + String.valueOf(currentClosestDistance));
			
		}
		
		return currentClosestID;
		
	}

}
